package com.sdhs.util;

import com.sdhs.common.utils.spring.SpringUtils;
import com.sdhs.costcontrol.domain.GcControlContractPay;
import com.sdhs.costcontrol.service.IGcControlContractPayService;
import com.sdhs.myproject.domain.GcMyprojectContractInformation;
import com.sdhs.myproject.domain.GcMyprojectInformation;
import com.sdhs.myproject.domain.GcMyprojectProfitMarginMeasurement;
import com.sdhs.myproject.service.IGcMyprojectContractInformationService;
import com.sdhs.myproject.service.IGcMyprojectInformationService;
import com.sdhs.myproject.service.IGcMyprojectProfitMarginMeasurementService;
import com.sdhs.projectschedule.domain.GcProgressPaymentCollectionTracking;
import com.sdhs.projectschedule.service.IGcProgressPaymentCollectionTrackingService;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * add
 * 修改我的项目-利润率测算-实施期利润率
 * */
public class BidUtilization {

    private static final IGcMyprojectProfitMarginMeasurementService gcProfitMarginMeasurementService = SpringUtils.getBean(IGcMyprojectProfitMarginMeasurementService.class);
    private static final IGcMyprojectInformationService myProjectInformationService = SpringUtils.getBean(IGcMyprojectInformationService.class);
    private static final IGcProgressPaymentCollectionTrackingService progressPaymentCollectionTrackingService = SpringUtils.getBean(IGcProgressPaymentCollectionTrackingService.class);
    private static final IGcMyprojectContractInformationService myProjectContractInformationService = SpringUtils.getBean(IGcMyprojectContractInformationService.class);
    private static final IGcControlContractPayService controlContractPayService = SpringUtils.getBean(IGcControlContractPayService.class);

    public static void UpdateBidUtilization(Long projectId) {
        System.out.println("implement_utilization");

        GcMyprojectInformation gcMyprojectInformation = myProjectInformationService.selectImplementUtilizationById(projectId);
        BigDecimal paidBack;
        BigDecimal paymentAmount;
        BigDecimal costAmount;
        if (gcMyprojectInformation == null) {
             paidBack =  BigDecimal.ZERO;   // 回款填报总金额
             paymentAmount =  BigDecimal.ZERO;  // 支付跟踪总金额
             costAmount =  BigDecimal.ZERO;  // 管理成本总金额
        }else{
             paidBack = gcMyprojectInformation.getPaidBack();   // 回款填报总金额
             paymentAmount = gcMyprojectInformation.getPaymentAmount();  // 支付跟踪总金额
             costAmount = gcMyprojectInformation.getCostAmount();  // 管理成本总金额
        }
        BigDecimal ImplementUtilization = BigDecimal.ZERO;  // 默认值 0

        // 检查 paidBack 是否为 null 或 0
        if (paidBack != null && paidBack.compareTo(BigDecimal.ZERO) != 0) {
            // 计算 (paidBack - paymentAmount - costAmount) / paidBack
            BigDecimal numerator = paidBack.subtract(paymentAmount != null ? paymentAmount : BigDecimal.ZERO)
                    .subtract(costAmount != null ? costAmount : BigDecimal.ZERO);
            BigDecimal tempUtilization = numerator.divide(paidBack, 4, RoundingMode.HALF_UP); // 保留4位小数

            // 如果计算结果 > 0，则赋值；否则保持 0
            if (tempUtilization.compareTo(BigDecimal.ZERO) > 0) {
                ImplementUtilization = tempUtilization.multiply(BigDecimal.valueOf(100)); // 乘以 100
            }
        }

        // 更新数据库
        GcMyprojectProfitMarginMeasurement gcMyprojectProfitMarginMeasurement = new GcMyprojectProfitMarginMeasurement();
        gcMyprojectProfitMarginMeasurement.setProjectId(projectId);
        gcMyprojectProfitMarginMeasurement.setImplementUtilization(ImplementUtilization.toString());
        gcProfitMarginMeasurementService.updateMeasurement(gcMyprojectProfitMarginMeasurement);
    }
        /**回款跟踪，回款填报和删除时修改我的项目-合同信息-累计值*/
    public static void UpdateProjectContract(Long projectId, String contractId) {
         GcProgressPaymentCollectionTracking collectionTracking = progressPaymentCollectionTrackingService.selectContractByProjectId(projectId,contractId);
        GcMyprojectContractInformation gcProgressPaymentCollection = new GcMyprojectContractInformation();
        gcProgressPaymentCollection.setContractId(collectionTracking.getContractId()); // 合同id
        gcProgressPaymentCollection.setProjectId(projectId); // 项目id
        gcProgressPaymentCollection.setInvoicedAmount(new BigDecimal(collectionTracking.getSumInvoicedOut())); // 已开票金额
        gcProgressPaymentCollection.setProportionInvoicedMounts(new BigDecimal(collectionTracking.getSumInvoicedOutProportion())); // 已开票比例
        gcProgressPaymentCollection.setAmountAlreadyReceived(new BigDecimal(collectionTracking.getSumPaidBack())); // 已收款金额
        gcProgressPaymentCollection.setProportionPaymentsReceived(new BigDecimal(collectionTracking.getSumPaidBackProportion())); // 已收款比例

        gcProgressPaymentCollection.setNotInvoicedAmount(collectionTracking.getNotInvoicedAmount());// 已开票金额（不含税）
        gcProgressPaymentCollection.setUninvoicedAmount(collectionTracking.getUninvoicedAmount());// 未开发票金额（含税）
        gcProgressPaymentCollection.setProportionUnissuedInvoices(collectionTracking.getProportionUnissuedInvoices());// 未开发票比例
        gcProgressPaymentCollection.setAmountRevenueRecognized(collectionTracking.getAmountRevenueRecognized());// 收入确认金额(收入类合同)
        gcProgressPaymentCollection.setNotAmountRevenueRecognized(collectionTracking.getNotAmountRevenueRecognized());// 收入确认金额(不含税收入类合同)
        gcProgressPaymentCollection.setRevenueRecognitionRatio(collectionTracking.getRevenueRecognitionRatio());// 收入确认比例(收入类合同)
        gcProgressPaymentCollection.setAccountsReceivable(collectionTracking.getAccountsReceivable());// 应收账款(收入类合同)
        gcProgressPaymentCollection.setContractAssets(collectionTracking.getContractAssets());// 合同资产(收入类合同)
        /*调用我的合同修改累计数据信息*/
        myProjectContractInformationService.updateGcMyProjectContractInformationByProject(gcProgressPaymentCollection);
    }
    /**支付跟踪，支付填报，修改和删除时修改我的项目-合同信息-累计值*/
    public static void UpdateMyProjectContract(Long projectId, String contractId) {
        GcControlContractPay gcControlContractPay = controlContractPayService.selectControlContractPayByProjectId(projectId, contractId);
        GcMyprojectContractInformation gcProgressPaymentCollection = new GcMyprojectContractInformation();
        gcProgressPaymentCollection.setContractId(contractId); // 合同id
        gcProgressPaymentCollection.setProjectId(projectId); // 项目id
        gcProgressPaymentCollection.setAmountPaid(gcControlContractPay.getTotalPaymentAmount()); // 已回款金额
        gcProgressPaymentCollection.setPercentagePaymentRatio(gcControlContractPay.getTotalPaymentRatio()); // 已回款比例
        gcProgressPaymentCollection.setInvoicedAmount(gcControlContractPay.getTotalInvoiceAmount()); // 已开票金额
        gcProgressPaymentCollection.setProportionInvoicedMounts(gcControlContractPay.getTotalInvoiceRatio()); // 已开票比例

        gcProgressPaymentCollection.setNotInvoicedAmount(gcControlContractPay.getNotInvoicedAmount()); // 已开票金额（不含税）
        gcProgressPaymentCollection.setUninvoicedAmount(gcControlContractPay.getUninvoicedAmount()); // 未开发票金额（含税）
        gcProgressPaymentCollection.setProportionUnissuedInvoices(gcControlContractPay.getProportionUnissuedInvoices()); // 未开发票比例
        gcProgressPaymentCollection.setCostConfirmationAmount(gcControlContractPay.getCostConfirmationAmount()); // 成本确认金额(支出类合同)
        gcProgressPaymentCollection.setNotCostConfirmationAmount(gcControlContractPay.getNotCostConfirmationAmount()); // 成本确认金额(支出类合同)
        gcProgressPaymentCollection.setCostConfirmationRatio(gcControlContractPay.getCostConfirmationRatio()); // 成本确认比例(支出类合同)
        gcProgressPaymentCollection.setAccountsPayable(gcControlContractPay.getAccountsPayable());//应付账款(支出类合同)
        /*调用我的合同修改累计数据信息*/
        myProjectContractInformationService.updateGcMyProjectContractInformationByProject(gcProgressPaymentCollection);
    }
}
